package com.twu.biblioteca.model;

import com.twu.biblioteca.constant.BibliotecaConstans;
import com.twu.biblioteca.exception.BookNotAvailableException;
import com.twu.biblioteca.exception.MovieNotAvailableException;
import com.twu.biblioteca.exception.ReturnBookNotAvailableException;

import java.util.ArrayList;
import java.util.List;

public class Biblioteca {
    private List<BibliotecaMovie> movieList;
    private String name;
    private List<BibliotecaBook> books;
    private BibliotecaUser currentUser;

    public Biblioteca() {
        books = new ArrayList<BibliotecaBook>();
        movieList = new ArrayList<BibliotecaMovie>();
    }

    public List<BibliotecaBook> getBooks() {
        return books;
    }

    public void addBook(BibliotecaBook book) {
        book.setStatus(BibliotecaConstans.STATUS_IN_BIBLIOTECA);
        book.setBelongTo(name);
        books.add(book);
    }

    private BibliotecaBook findBook(String bookName) {
        for (BibliotecaBook book:books){
            if (book.getName().equalsIgnoreCase(bookName)){
                return book;
            }
        }
        return null;
    }

    public BibliotecaBook checkoutBook(String bookName) throws BookNotAvailableException {
        BibliotecaBook book = findBook(bookName);
        if (!isAvailableBook(book)){
            throw new BookNotAvailableException();
        }
        book.setStatus(BibliotecaConstans.STATUS_OUT_BIBLIOTECA);
        currentUser.addBook(book);
        return book;
    }

    public boolean isAvailableBook(BibliotecaBook book) {
        return books.contains(book) && book.isInBiblioteca();
    }

    private boolean isReturnAvailableBook(BibliotecaBook book) {
        return books.contains(book) && !book.isInBiblioteca();
    }

    public void checkinBook(String bookName) throws ReturnBookNotAvailableException {
        BibliotecaBook book = findBook(bookName);
        if (!isReturnAvailableBook(book)){
            throw new ReturnBookNotAvailableException();
        }
        book.setStatus(BibliotecaConstans.STATUS_IN_BIBLIOTECA);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<BibliotecaMovie> getMovieList() {
        return movieList;
    }

    public void addMovie(BibliotecaMovie movie) {
        movie.setStatus(BibliotecaConstans.STATUS_IN_BIBLIOTECA);
        movieList.add(movie);
    }

    public BibliotecaMovie checkoutMovie(String name) throws MovieNotAvailableException {
        BibliotecaMovie movie = findMovie(name);
        if (!isCheckoutAvailableMovie(movie)){
            throw new MovieNotAvailableException();
        }
        movie.setStatus(BibliotecaConstans.STATUS_OUT_BIBLIOTECA);
        currentUser.addMovie(movie);
        return movie;
    }

    private boolean isCheckoutAvailableMovie(BibliotecaMovie movie) {
        return movie!=null && movie.getStatus().equalsIgnoreCase(BibliotecaConstans.STATUS_IN_BIBLIOTECA);
    }

    private BibliotecaMovie findMovie(String name) {
        for (BibliotecaMovie movie:movieList){
            if (movie.getName().trim().equalsIgnoreCase(name)){
                return movie;
            }
        }
        return null;
    }

    public void setLoginUser(BibliotecaUser loginUser) {
        this.currentUser = loginUser;
    }

    public BibliotecaUser getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(BibliotecaUser currentUser) {
        this.currentUser = currentUser;
    }
}
